﻿   ﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iPick.Models;
using System.Web.Http.Data.EntityFramework;
namespace iPick.Controllers
{
    public class DataServiceController
    {

        dbSQLIPickEntities _db;
        public List<Catalog> GetCatalogsByDeviceKey(CustomerDevice customerDevice)
        {

            _db = new iPick.dbSQLIPickEntities();
            // To Do get the Customer Id By Device Id
  return _db.GetRandomCatalogs(customerDevice.CustomerDevicesID).ToList<Catalog>();

        }
        public CustomerDevice GetCustomerDeviceByDeviceKey(string DeviceKey,string DeviceName)
        {

            _db = new iPick.dbSQLIPickEntities();
            CustomerDevice myCustomerDevice = _db.CustomerDevices.Where(d => d.DeviceKey == DeviceKey).FirstOrDefault();
            if(myCustomerDevice== null)
            {  Customer myNewCustomer = new Customer
                {
                    FirstName=DeviceKey,
                    LastName=DeviceName,
                    Email ="support@ipickmovies.com",
                    PWD = "",
                    DateCreated = System.DateTime.Now
                };
                this.CreateNewCustomer(myNewCustomer);
           

                _db = new iPick.dbSQLIPickEntities();

                myCustomerDevice = new CustomerDevice();
                myCustomerDevice.DeviceKey = DeviceKey;
                myCustomerDevice.DeviceName = DeviceName;
                myCustomerDevice.DateCreated = System.DateTime.Now;
                myCustomerDevice.NumberOfItemsToDisplay = 1;
                myCustomerDevice.CustomerID = myNewCustomer.CustomerId;
                _db.CustomerDevices.AddObject(myCustomerDevice);
                _db.SaveChanges();

            }
            _db.Connection.Close();
            _db = null;
            return myCustomerDevice;
        }

        public CustomerDevice UpdateCustomerDevice(string deviceKey, CustomerDevice customerDevice)
        {

            _db = new iPick.dbSQLIPickEntities();
            CustomerDevice myCustomerDevice = _db.CustomerDevices.Where(d => d.DeviceKey == deviceKey).FirstOrDefault();
            if (myCustomerDevice != null)
                myCustomerDevice.NumberOfItemsToDisplay = customerDevice.NumberOfItemsToDisplay;
            else
            {
                //create a new customer device key if the information is available
                CustomerDevice oNewCustomerDevice = new CustomerDevice();
                oNewCustomerDevice.DeviceKey = customerDevice.DeviceKey;
                oNewCustomerDevice.DeviceName = customerDevice.DeviceName;
                oNewCustomerDevice.DateCreated = System.DateTime.Now;
                oNewCustomerDevice.NumberOfItemsToDisplay = customerDevice.NumberOfItemsToDisplay;
                oNewCustomerDevice.CustomerID = customerDevice.CustomerID;
                _db.CustomerDevices.AddObject(oNewCustomerDevice);

                myCustomerDevice = oNewCustomerDevice;
            }


            _db.SaveChanges();

            _db.Connection.Close();
            _db = null;
            return myCustomerDevice;
        }


        public Customer getCustomerbyEmailAddress(string emailAddress)
        {
            _db = new iPick.dbSQLIPickEntities();
            return _db.Customers.Where(c => c.Email.ToLower() == emailAddress.ToLower()).FirstOrDefault();

        }
        public bool CreateNewCustomer(Customer myNewCustomer)
        {
            try
            {

                _db = new iPick.dbSQLIPickEntities();
                _db.Customers.AddObject(myNewCustomer);
                _db.SaveChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                _db.Connection.Close();
                _db = null;

            }
            return true;
        }

        #region "Customer Favorites"
        public CustomerFavorite CustomerFavorites_Add(long customerId,long catalogId)
        {
            _db = new iPick.dbSQLIPickEntities();

            CustomerFavorite oFavorite = new CustomerFavorite();
            oFavorite.CustomerID = customerId;
            oFavorite.CatalogID = catalogId;

            _db.CustomerFavorites.AddObject(oFavorite);
            _db.SaveChanges();

            _db.Connection.Close();
            _db = null;
            return oFavorite;
        }

        public void CustomerFavorites_Remove(long customerFavoriteID)
        {
            _db = new iPick.dbSQLIPickEntities();

            CustomerFavorite oFavorite = _db.CustomerFavorites.Where(c => c.CustomerFavoriteID == customerFavoriteID).FirstOrDefault();
          
            _db.CustomerFavorites.DeleteObject(oFavorite);
            _db.SaveChanges();

            _db.Connection.Close();
            _db = null;
            return;
        }
        public List<CustomerFavorite> CustomerFavorites_GetCustomerFavorites(string emailAddress)
        {
            _db = new iPick.dbSQLIPickEntities();
            //var result = _db.CustomerFavorites.Join(_db.Catalogs, cf=> cf.CatalogID, cat=> cat.CatalogId, (cf,cat) => new { CustomerFavorite cf});

            //var result2 = from cf in _db.CustomerFavorites
            //              //join cus in _db.Customers on cf.CustomerID equals cus.CustomerId
            //              //join cat in _db.Catalogs on cf.CatalogID equals
            //              select cf;

            var result = _db.CustomerFavorites.Where(c => c.Customer.Email.ToLower() == emailAddress.ToLower());

//            var content=
//from category  in _contentRepository.Category
//join  contentCategory in _contentRepository.ContentCategory
//on category.category_id equals contentCategory.category_id 
//join content in _contentRepository.Content
//on content.content_id equals contentCategory.content_id
//where category.category_id==@yourcategoryId 
//select new {type , title  }



            //result = null;


                //dbo.Companies.Join(dbo.Persons, c => c.AccountCoordinatorPersonId, p => p.PersonId, (c, p) => new { Company c, AccountCoordinator = p.FirstName + ' ' + p.Surname })
             //.Join(dbo.Persons, c => c.Company.AccountManagerPersonId, p2 => p2.PersonId, (c, p2) => new { c.Company.CompanyId, c.Company.CompanyName, c.AccountCoordinator, AccountManager = p2.FirstName + ' ' + p2.Surname);

            return result.ToList(); // _db.Customers.Where(c => c.Email.ToLower() == emailAddress.ToLower()).FirstOrDefault();

        }
        #endregion
    }
}
